<?php
require ("../global.inc.php");
class OwnMemberManage extends SystemFrameWork{
	/**
	 * 会员对象
	 *
	 * @var obj
	 */
	var $obj_member;
	/**
	 * 验证对象
	 *
	 * @var obj
	 */
	var $obj_validate;
	/**
	 * 商品对象
	 *
	 * @var obj
	 */
	var $obj_product;

	/**
	 * 权限对象
	 *
	 * @var obj
	 */
	var $obj_system_power;

	 /**
	  * 部门对象
	  *
	  **/
	 var $obj_department;
	 
	 /**
	  * 分页对象
	  *
	  **/
	 var $obj_page;
	 	
	function main(){
		/**
		 * 创建会员对象
		 */
		if (!is_object($this->objmember)){
			require_once ("member.class.php");
			$this->obj_member = new MemberClass();
		}
		/**
		 * 创建验证对象
		 */
		if (!is_object($this->objvalidate)){
			require_once("commonvalidate.class.php");
			$this->obj_validate = new CommonValidate();
		}
		/**
		 * 创建权限对象
		 */
		if (!is_object($this->obj_system_power)){
			require_once("system_power.class.php");
			$this->obj_system_power = new SystemPowerClass();
		}
		/**
		 * 创建部门对象
		 **/
		if (!is_object($this->ojb_department))
		{
			require_once('department.class.php');
			$this->obj_department = new DepartmentClass();
		}
		/**
		 * 初始化分页类
		 */
		if (!is_object($this->obj_page)){
			require_once("commonpage.class.php");
			$this->obj_page = new CommonPage();
		}
		/**
		 * 语言包
		 */
		$this->getlang("sys_member.manage");
		
		/**
		 * 执行操作
		 */
		switch($this->_input['action']){
			case "member":
				$this->_getListInfo();
				break;
			case "ajax_list":
				$this->_ajax_list();
				break;
			case "add_member":
				$this->_add_member();
				break;
			case 'modi_member':
				$this->_modi_member();
				break;
			case 'del_pic':
				$this->_del_pic();
				break;
			case "update_member":
				$this->_update_member();
				break;
			case 'save_member':
				$this->_save_member();
				break;
			case "modifysave":
				$this->_saveModifyBaseInfo();
				break;
			case "ajax_get_member":
				$this->_ajax_get_member();
				break;
			case "del_member":
				$this->_del_member();
				break;
			case "ajaxmembersbydept":
				$this->_ajaxmembersbydept();
				break;
			default:
				$this->_getListInfo();
				break;
		}

	}

	/**
	 * 保存更新管理员
	 */
	function _update_member(){

		/**
		 * 验证接收变量
		 */
		if ($this->_input["sys_password"] != "" || $this->_input["sys_password_two"] != ""){
			$this->obj_validate->setValidate(array("input"=>$this->_input['sys_password'],"require"=>"true","message"=>$this->_lang['errMpassword']));
			$this->obj_validate->setValidate(array("input"=>$this->_input['sys_password_two'],"require"=>"true","message"=>$this->_lang['errMpassword']));
			$this->obj_validate->setValidate(array("input"=>$this->_input['sys_password'],"validator"=>"Length", "min"=>"5", "max"=>"16", "message"=>$this->_lang['errWrongPassword']));
			$this->obj_validate->setValidate(array("input"=>$this->_input["sys_password_two"], "validator"=>"Length", "min"=>"5", "max"=>"16", "message"=>$this->_lang['errWrongPassword']));
			$this->obj_validate->setValidate(array("input"=>$this->_input["sys_password"], "require"=>"true", "validator"=>"Compare", "operator"=>"==","to"=>$this->_input["sys_password_two"], "message"=>$this->_lang['errMmatchingpassword'],"result"=>true));
		}
		$this->obj_validate->setValidate(array("input"=>$this->_input['sys_email'],"require"=>"true","validator"=>"Email","message"=>$this->_lang['errEmail']));
		$error = $this->obj_validate->validate();
		if($error != ""){
			$this->redirectPath('error','',$error,1);
		}
		/**
		 * 更新
		 */
		if ($this->obj_member->updateSysMember($this->_input)) {
			//记录操作日志
			SystemPowerClass::addSysLog('修改员工信息');
			$this->redirectPath('error','./member.manage.php?action=ajax_member','修改成功',1);
		}else {
			$this->redirectPath('error','','修改失败',1);
		}
	}

	function _add_member()
	{
		/**
		 * 权限组列表
		 */
		$sys_group_list = $this->obj_system_power->getSystemGroupList('',$page);
        
		$department_list = $this->obj_department->getDepartmentList(array('status' => 1), $page);
		/**
		 * 页面输出
		 */
		$this->output('sys_group_list',$sys_group_list);
		$this->output('department_list', $department_list);
		$this->showpage('member.add');
	}
	
	function _save_member()
	{
		/**
		 * 验证接收变量
		 */
		$this->obj_validate->validateparam = array(
		array("input"=>$this->_input["sys_login_name"], "require"=>"true", "message"=>'登录用户名不能为空'),
		array("input"=>$this->_input["sys_login_name"], "validator"=>"Length", "min"=>"5", "max"=>"20", "message"=>'登录用户名格式错误'),
		array("input"=>$this->_input["real_name"], "require"=>"true", "message"=>'姓名不能为空'),
		array("input"=>$this->_input["sys_password"], "require"=>"true", "message"=>'密码不能为空'),
		array("input"=>$this->_input["sys_password_two"], "require"=>"true", "message"=>'确认密码不能为空'),
		array("input"=>$this->_input["sys_password"], "validator"=>"Length", "min"=>"5", "max"=>"16", "message"=>'密码长度错误'),
		array("input"=>$this->_input["sys_password_two"], "validator"=>"Length", "min"=>"5", "max"=>"16", "message"=>'确认密码长度错误'),
		array("input"=>$this->_input["sys_password"], "require"=>"true", "validator"=>"Compare", "operator"=>"==","to"=>$this->_input["sys_password_two"], "message"=>'两次密码输入不同',"result"=>true),
		array("input"=>$this->_input['sys_email'],"require"=>"true","validator"=>"Email","message"=>'邮箱输入错误')
		);
		$error = $this->obj_validate->validate();
		if($error != ""){
			$this->redirectPath('error','',$error,1);
		}
		//验证会员名是否重复
		$condition['sys_login_name'] = $this->_input['sys_login_name'];
		$member_list = $this->obj_member->getSystemMemberList($condition,$page);
		if (!empty($member_list)){
			$this->redirectPath('error','','用户名已存在',1);
		}
		/**
		 * 添加管理员
		 */
		$this->_input['pic'] = $this->_member_img_upload('pic');
		if ($this->obj_member->addSysMember($this->_input)) {
			//记录操作日志
			SystemPowerClass::addSysLog('添加员工');
			$this->redirectPath('error','./member.manage.php?action=ajax_member','添加成功',1);
		}else {
			$this->redirectPath('error','','添加失败',1);
		}
	}

	/**
	 * 修改管理员
	 */
	function _modi_member(){
		/**
		 * 取管理员信息
		 */
		$sys_member_id = $this->_input['sys_member_id'];
		$member_array = $this->obj_member->getSysMemberById($sys_member_id);
		/**
		 * 权限组列表
		 */
		$sys_group_list = $this->obj_system_power->getSystemGroupList('',$page);
		$department_list = $this->obj_department->getDepartmentList(array('status' => 1), $page);
		/**
		 * 页面输出
		 */
		$this->output('department_list', $department_list);
		$this->output('member_array',$member_array);
		$this->output('sys_group_list',$sys_group_list);
		$this->showpage('member.modi');
	}

	/**
	 * 删除品牌图片
	 */
	function _del_pic(){
		if ($this->_input['pic'] != ''){
			//删除的图片地址
			$value_array['sys_member_id'] = intval($this->_input['sys_member_id']);
			if ($this->_input['sys_password'] != '')
			{
				$value_array['sys_password']   = $this->_input['sys_password'];
			}
			$value_array['pic']    		   = '';
			$value_array['real_name']     = $this->_input['real_name'];
			$value_array['sys_group_id']   = $this->_input['sys_group_id'];
			$value_array['sys_email']      = $this->_input['sys_email'];
			$value_array['gender']         = $this->_input['gender'];
			$value_array['txtBirthday']       = $this->_input['txtBirthday'];
			$value_array['department']  = $this->_input['department'];
			$value_array['ext']            = $this->_input['ext'];
			$result = $this->obj_member->updateSysMember($value_array);
			unset($update_arr);
			if ($result !== true) {
				$this->redirectPath("error","",'删除失败',1);
			}
			//将传过来的图片名称分别整理为原有图片，中号和大号图片,全部删除
			if (file_exists(BasePath.'/'.$this->_input['pic'])){
				@unlink(BasePath.'/'.$this->_input['pic']);
			}
			$this->redirectPath("error","",'删除成功',1);
		}else {
			//输出信息
			$this->redirectPath("error","",'删除失败',1);
		}
	}

	/**
	 * 得到会员列表
	 *
	 */
	function _getListInfo(){
		$this->_ajax_list();
	}

	/**
	 * ajax 分页
	 */
	function _ajax_list(){
		/**
		 * 获得列表
		 */
		$this->obj_page->pagebarnum(12);
		$this->obj_page->nowindex = $this->_input['curpage']?$this->_input['curpage']:1;
		$condition['sys_group_id'] = $this->_input['sys_group_id'];
		$condition['order_group_id'] = 1;
		$sys_member_list = $this->obj_member->getSystemMemberList($condition,$this->obj_page);
		if ($this->_input['sys_group_id'] !='' ){
			/*取权限组名称*/
			$group_array = $this->obj_system_power->getSysGroupById($this->_input['sys_group_id']);
			if (is_array($sys_member_list)){
				foreach ($sys_member_list as $k => $v){
					$sys_member_list[$k]['sys_group_name'] = $group_array['sys_group_name'];
				}
			}
			unset($group_array);
		}else {
			if (is_array($sys_member_list)){
				foreach ($sys_member_list as $k => $v){
					$array = $this->obj_system_power->getSysGroupById($v['sys_group_id']);
					$sys_member_list[$k]['sys_group_name'] = $array['sys_group_name'];
					unset($array);
				}
			}
		}
		$page_list = $this->obj_page->show(2);
		if ($this->obj_page->nowindex == ''){
			$this->obj_page->nowindex = 1;
		}
		//权限组列表
		$group_array = $this->obj_system_power->getSystemGroupList('',$obj_page);
		/**
		 * 页面输出
		 */
		$this->output("curpage", $this->obj_page->nowindex);
		$this->output('sys_member_list',$sys_member_list);
		$this->output('page_list',$page_list);
		$this->output('group_array',$group_array);
		$this->output('sys_group_id',$this->_input['sys_group_id']);
		$this->showpage('member.list');
	}

	/**
	 * 删除会员
	 */
	function _del_member(){
		if (!empty($this->_input['member_id'])) {
			
			if (is_array($this->_input['member_id'])) {
				foreach ($this->_input['member_id'] as $v){
					$result = $this->obj_member->_setMemberState($v,2);
				}
			}else {
				$result = $this->obj_member->_setMemberState($this->_input['member_id'],2);
				
			}
			if ($result !== true) {
				$this->redirectPath('error','','删除员工失败',1);
			}
			/*记录操作日志*/
			SystemPowerClass::addSysLog('系统->员工管理->删除员工');
			$this->redirectPath('error','','删除员工成功',1);
		}else {
			$this->redirectPath('error','','参数错误！',1);
		}
	}
	
	
	
	/**
	 * 返回页面的形式为 option格式
	 */
	function _ajax_get_member(){
		
//		if (!is_object($obj_ajax_page)) {
//			require_once("ajaxcommonpage.class.php");
//			$obj_ajax_page = new ajaxCommonPage();
//		}
//		$obj_ajax_page->pagebarnum(10);
		$member_name = Common::unescape($this->_input['txtMemberName'],$this->_configinfo['websit']['ncharset']);
		$dept_id = $this->_input['deptid'];
		$where = 'where 1=1 ';
		if ($member_name != '') {
			$where .= " and sys_login_name like '%".$member_name."%' ";
		}
		if ($dept_id) {
			$where .= " and department_id = '".$dept_id."' ";
		}
		$member_array = $this->obj_member->listMember($where, $obj_ajax_page);
//		$page_list = $obj_ajax_page->show(2);

		/**
		 * 页面输出
		 */
//		$this->output('page_list',$page_list);
		$this->output('member_array',$member_array);
		$this->showpage("sys_module_member.result");
	}
	/**
	 * 员工图片上传
	 */
	function _member_img_upload($img){
		if (isset($_FILES[$img]['name']) && $_FILES[$img]['name'] != '') {
			require_once("uploadfile.class.php");
			$upload = new UploadFile();
			$upload->error_msg_type = 1;
			$upload->allow_type = explode(',',$this->_fileconfig['allowuploadimagetype']);
			//上传图片
			$filename = $upload->upfile($img);
			unset($upload);
			return $filename["getfilename"];
		}
	}
	
	/**
	 * 根据部门获取员工
	 */
	function _ajaxmembersbydept(){
		/**
		 * 取管理员信息
		 */
		$dept_id = $this->_input['dept_id'];
		$member_array = $this->obj_member->listMember("where department_id=".$dept_id, $obj_page);
		$str = '';
		$tmp_array = array();
		if ($member_array) {
			foreach ($member_array as $val){
				$tmp_array[] = array('member_id'=>$val['sys_member_id'], 'real_name'=>$val['real_name']);
			}
			$str = json_encode($tmp_array);
		}
		echo $str;
	}
}
$member = new OwnMemberManage();
$member->main();
unset($member);
?>